package com.ljh.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljh.pojo.Collection;
import com.ljh.pojo.Singer;
import com.ljh.pojo.Song;
import com.ljh.service.CollectionService;
import com.ljh.service.Impl.SongServiceImpl;
import com.ljh.service.ListService;
import com.ljh.service.SongService;
import com.ljh.utils.Result;
import com.ljh.vo.SexVo;
import com.ljh.vo.SongListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("song")
@Api(description = "歌曲接口管理")
@CrossOrigin
public class SongController {

    @Autowired
    SongService songService;

    @Autowired
    ListService listService;

    /**
     * 随机排序去前num位
     * @param num
     * @return
     */

    @GetMapping("findSongs")
    @ApiOperation(value = "随机找num个歌曲")
    public Result find(Integer num){
        return Result.success(songService.findNineSongs(num));
    }

    /**
     * 通过song的id查找歌曲
     * @param songId
     * @return
     */
    @GetMapping("find")
    @ApiOperation("通过song的id查找歌曲")
    public Song findSongById(Integer songId){
        System.out.println(songId);
        return songService.findSongById(songId);
    }

    /**
     * 更新听歌的次数,每听一次加一
     * @param num
     * @param id
     * @return
     */
    @GetMapping("updateSongNum")
    @ApiOperation(value = "更新听歌的次数,每听一次加一")
    public String updateSongNum(Integer num,Integer id){
        num++;
        songService.updateSongNum(num,id);
        return "a";
    }

    /**
     * 搜索查询,模糊
     * @param str
     * @return
     */
    @GetMapping(value = "findMuhu")
    @ApiOperation(value = "搜索查询,模糊")
    public Result findMohu(String str){
        List<Song> mohu = songService.findMohu(str);
        return Result.success(mohu);
    }

    /**
     * 通过歌曲类型找歌
     * @param str
     * @return
     */
    @GetMapping("findStyle")
    @ApiOperation(value = "通过歌曲类型找歌")
    public Result findStyle(String str){
        List<SongListVo> songListByStyle = songService.findSongListByStyle2(str);
        return Result.success(songListByStyle);
    }


    @GetMapping("/findSong")
    @ApiOperation(value = "歌曲列表")
    public Result songList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String songName
    ) {
        IPage<Singer> page = songService.page(new Page(current,size),new QueryWrapper<Song>()
                .like(StrUtil.isNotBlank(songName), "songName",songName));
        return Result.success(page);
    }

    @GetMapping("/deleteSong")
    @ApiOperation(value = "删除歌曲")
    public Result deleteSong(
            @RequestParam Integer songId
    ){
        listService.deleteListSongBySongId(songId);
        songService.deleteSongById(songId);
        return Result.success("修改成功");
    }

    @GetMapping("/findSongBySongId")
    @ApiOperation(value = "通过音乐id来查找")
    public Result findSongBySongId(
            @RequestParam Integer songId
    ){
        List<Song> songBySongId = songService.findSongBySongId(songId);
        return Result.success(songBySongId);
    }

    @GetMapping("/updatePhoto")
    @ApiOperation(value = "更新照片")
    public Result updatePhoto(String songPhoto,Integer songId){
        songService.UpdatePersonPhoto(songPhoto, songId);
        return Result.success("操作成功");
    }

    @GetMapping("/updateUrl")
    @ApiOperation(value = "更新音乐链接")
    public Result updateUrl(String songUrl,Integer songId){
        songService.updateSong(songUrl, songId);
        return Result.success("修改成功");
    }

    @GetMapping("/insertSong")
    @ApiOperation(value = "加入音乐")
    public Result insertSong(
            @RequestParam String singerName,
            @RequestParam Integer singerId,
            @RequestParam String songName,
            @RequestParam String songStyle
    ){
        songService.insertSong(singerName, singerId, songName, songStyle);
        return Result.success("修改成功");
    }

    @GetMapping("/findSongStyle")
    @ApiOperation(value = "根据歌曲的风格分类")
    public Result findSongStyle(){
        List<SexVo> songStyle = songService.findSongStyle();
        return Result.success(songStyle);
    }

    @GetMapping("/findSingerHot")
    @ApiOperation(value = "对于播放次数求和排序")
    public Result findSingerHot(){
        return Result.success(songService.findSingerHot());
    }

    @GetMapping("/findHotSong")
    @ApiOperation(value = "播放前十")
    public Result findHotSong(){
        return Result.success(songService.findHotSong());
    }
}
